package com.example.miniappssm.service.serviceImpl;

import com.example.miniappssm.bean.Goods;
import com.example.miniappssm.mapper.GoodsMapper;
import com.example.miniappssm.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    /**
     * 查询所有商品
     * @author codertao
     * @date 2022/5/21 12:16
     * @return java.util.List<java.util.List<com.example.miniappssm.bean.Goods>>
     */
    @Override
    public List<List<Goods>> selectAllGoods() {
        List<List<Goods>> allGoods = new ArrayList<>();
        for (int i = 0; i < 4; i ++) {
            allGoods.add(goodsMapper.selectGoodsByGoodsType(i));
        }
        return allGoods;
    }

    @Override
    public Goods selectGodsByGoodsId(Integer goodsId) {
        return goodsMapper.selectGoodsByGoodsId(goodsId);
    }

    @Override
    public void updateGoods(Goods goods) {
        if (goods.getGoodsImg() == null || goods.getGoodsImg().equals("")) {
            goods.setGoodsImg(goodsMapper.selectGoodsByGoodsId(goods.getGoodsId()).getGoodsImg());
        }
        goodsMapper.updateGoods(goods);
    }

    @Override
    public void insertGoods(Goods goods) {
        goodsMapper.insertGoods(goods);
    }

    @Override
    public void removeGoodsByGoodsId(Integer goodsId) {
        goodsMapper.deleteGoodsByGoodsId(goodsId);
    }
}
